import QuestSpawn from "../QuestSpawn";
import LoadUtil from "../../Fantasy/Util/LoadUtil";
import SoundUtil from "../../Fantasy/Util/SoundUtil";
import JayUtil from "../../Fantasy/Util/JayUtil";
import ServerCenter from "../../Server/ServerCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SelectQuestJs extends cc.Component {
    @property(cc.Label)
    title: cc.Label = null;
    @property(cc.Node)
    voice:cc.Node = null;
    @property(cc.Node)
    string:(cc.Node) = null;
    @property(cc.Node)
    picture:cc.Node = null;
    @property(cc.Node)
    answersNode:cc.Node = null;
    @property(cc.Node)
    pictureNode:cc.Node = null;
    @property(cc.Prefab)
    answerButton:cc.Prefab = null;
    @property(cc.Prefab)
    answerPic:cc.Prefab = null;
    gameJs;
    buttonArrValue = [,,,];
    buttonArr = [];
    bookId;
    unit;
    voiceWord;
    answerNum;
    onEnable(){
        if(QuestSpawn.isWrong == true){
            Log("答案数组：",this.buttonArrValue);
            for(let i= 0;i<this.buttonArrValue.length;i++){
                if(this.buttonArrValue[i] == true){
                    Log("正确答案",i,"有几个按钮",this.buttonArr.length);
                    this.buttonArr[i].getChildByName("true").active = true;
                }
            }
        }
    }
    onDisable(){
       
        SoundUtil.stopAllEffect();
    }
    init(nodeJs,wordsList,bookId,unit){
        this.gameJs = nodeJs;
        this.bookId = bookId;
        this.unit = unit;
        let type =Math.floor(Math.random()*6);//
        if(Object.keys(wordsList).length > 14){
            this.answerNum = 4;
        }else{
            this.answerNum = 2;
        }
        if(type == 0){
            this.title.string = "看中文、选出正确的英文";
            var wordsArr = this.getAnserWords(wordsList,this.answerNum);
            this.selectWordByChinese(wordsArr);
        }else if(type == 1){
            this.title.string = "看英文、选出正确的中文含义";
            var wordsArr = this.getAnserWords(wordsList,this.answerNum);
            this.selectChineseByWord(wordsArr);
        }else if(type == 2){
            this.title.string = "听录音，选出听到的单词";
            var wordsArr = this.getAnserWords(wordsList,this.answerNum);
            this.selectWordByVoice(wordsArr);
        }else if(type == 3){
            this.title.string = "看图片、选出合适的英文";
            var wordsArr = this.getAnserWords(wordsList,this.answerNum);
            this.selectWordByPicture(wordsArr);
            // this.selectWordByPicture(this.selectWord(wordsList),wordsList);
        }else if(type == 4){
            this.title.string = "听录音、选出符合英文内容的图片";
            var wordsArr = this.getAnserWords(wordsList,this.answerNum);
            this.selectPictureByVoice(wordsArr);
            // this.selectWordByPicture(this.selectWord(wordsList),wordsList);
         }else if(type == 5){
            this.title.string = "看单词、匹配合适的图片";
            var wordsArr = this.getAnserWords(wordsList,this.answerNum);
            this.selectPictureByWord(wordsArr);
            // this.selectWordByPicture(this.selectWord(wordsList),wordsList);
        }

    }
    updatebuttonArrValue(index,bool){
        this.buttonArrValue[index] = bool;
    }
    updateButtonArr(node){
        this.buttonArr.push(node);
    }

    selectWordByChinese(wordsArr){//中文选英文
        this.string.active = true;
        var questExp = 3;
        var index = Math.floor(Math.random()*wordsArr.length);
        for(let i = 0;i < wordsArr.length;i ++){
            if(index == i){
                this.string.getComponent(cc.Label).string = wordsArr[i].translation;
                var node = cc.instantiate(this.answerButton);
                node.getChildByName("label").getComponent(cc.Label).string = wordsArr[i].words
                node.getComponent("SelectButtonJs").setIsTure(this.gameJs,true,questExp);
                this.updatebuttonArrValue(i,true);
                node.parent = this.answersNode.getChildByName("select");
            }else{
                var node = cc.instantiate(this.answerButton);
                node.getChildByName("label").getComponent(cc.Label).string = wordsArr[i].words
                node.getComponent("SelectButtonJs").setIsTure(this.gameJs,false,questExp);
                this.updatebuttonArrValue(i,false);
                node.parent = this.answersNode.getChildByName("select");
            }
            this.updateButtonArr(node);
        }
    }

    selectChineseByWord(wordsArr){//英文选中文
        var questExp = 3;
        this.string.active = true;
        var index = Math.floor(Math.random()*wordsArr.length);
        for(let i = 0;i < wordsArr.length;i ++){
            if(index == i){
                this.string.getComponent(cc.Label).string = wordsArr[i].words;
                var node = cc.instantiate(this.answerButton);
                node.getChildByName("label").getComponent(cc.Label).string = wordsArr[i].translation
                node.getComponent("SelectButtonJs").setIsTure(this.gameJs,true,questExp);
                this.updatebuttonArrValue(i,true);
                node.parent = this.answersNode.getChildByName("select");
            }else{
                var node = cc.instantiate(this.answerButton);
                node.getChildByName("label").getComponent(cc.Label).string = wordsArr[i].translation
                node.getComponent("SelectButtonJs").setIsTure(this.gameJs,false,questExp);
                this.updatebuttonArrValue(i,false);
                node.parent = this.answersNode.getChildByName("select");
            }
            this.updateButtonArr(node);

        }
    }

    selectWordByVoice(wordsArr){//声音选单词 
        var questExp = 5;
        this.voice.active = true;
        var index = Math.floor(Math.random()*wordsArr.length);
        for(let i = 0;i < wordsArr.length;i ++){
            if(index == i){
                var node = cc.instantiate(this.answerButton);
                this.voiceWord = wordsArr[i].words
                node.getChildByName("label").getComponent(cc.Label).string = this.voiceWord;
                //获取到音频并播放
                ServerCenter.getWordVoice(this.bookId,this.unit,this.voiceWord);
                node.getComponent("SelectButtonJs").setIsTure(this.gameJs,true,questExp);
                this.updatebuttonArrValue(i,true);
                node.parent = this.answersNode.getChildByName("select");
            }else{
                var node = cc.instantiate(this.answerButton);
                node.getChildByName("label").getComponent(cc.Label).string = wordsArr[i].words
                node.getComponent("SelectButtonJs").setIsTure(this.gameJs,false,questExp);
                this.updatebuttonArrValue(i,false);
                node.parent = this.answersNode.getChildByName("select");
            }
            this.updateButtonArr(node);
            
        }
    }
    voive(){
        if(!this.voiceWord){
            return;
        }
        Log("再说一遍")
        ServerCenter.getWordVoice(this.bookId,this.unit,this.voiceWord);
    }

    selectWordByPicture(wordsArr){ //图片选英文 只需要一张图片
        var questExp = 5;
        this.picture.active = true;
        if(wordsArr){
            var quest = this.getAnserWords(wordsArr,1);
            let container = this.picture.getChildByName("pic").getComponent(cc.Sprite)
            // LoadUtil.LoadNetImgJpg(this.picture.getChildByName("pic").getComponent(cc.Sprite),quest[0].wordsImg);
            ServerCenter.getWordImg(this.bookId,this.unit,container,quest[0].words);
            // var answerArr = JayUtil.shuffle(quest.concat(this.getAnserWords(wordsList,this.answerNum-1)));
            var answerArr = JayUtil.shuffle(wordsArr);
            for(let i = 0;i < answerArr.length;i ++){
                if(answerArr[i].wordId == quest[0].wordId){
                    var node = cc.instantiate(this.answerButton);
                    node.getChildByName("label").getComponent(cc.Label).string = answerArr[i].words
                    node.getComponent("SelectButtonJs").setIsTure(this.gameJs,true,questExp);
                    this.updatebuttonArrValue(i,true);
                    node.parent = this.answersNode.getChildByName("select");
                }else{
                    var node = cc.instantiate(this.answerButton);
                    node.getChildByName("label").getComponent(cc.Label).string = answerArr[i].words
                    node.getComponent("SelectButtonJs").setIsTure(this.gameJs,false,questExp);
                    this.updatebuttonArrValue(i,false);
                    node.parent = this.answersNode.getChildByName("select");
                }
                this.updateButtonArr(node);
            }

        }else{
            Log("没有带图片的单词");
        }
    }
    selectPictureByWord(wordsArr){//单词选图片 四张图
        var questExp = 7;
        this.string.active = true;
        // if(wordsArr.length > 4){
            var quest = JayUtil.shuffle(this.getAnserWords(wordsArr,this.answerNum));
            var index = Math.floor(Math.random()*quest.length);
            this.string.getComponent(cc.Label).string = quest[index].words;
            for(let i = 0;i < quest.length;i ++){
                if(quest[index].wordId == quest[i].wordId){
                    var node = cc.instantiate(this.answerPic);
                    let container = node.getChildByName("pic").getComponent(cc.Sprite);
                    // LoadUtil.LoadNetImgJpg(container,quest[i].wordsImg);
                    ServerCenter.getWordImg(this.bookId,this.unit,container,quest[i].words);
                    node.getComponent("SelectButtonJs").setIsTure(this.gameJs,true,questExp);
                    this.updatebuttonArrValue(i,true);
                    // node.parent = this.answersNode.getChildByName("picture");
                }else{
                    var node = cc.instantiate(this.answerPic);
                    let container = node.getChildByName("pic").getComponent(cc.Sprite);
                    // LoadUtil.LoadNetImgJpg(container,quest[i].wordsImg);
                    ServerCenter.getWordImg(this.bookId,this.unit,container,quest[i].words);
                    node.getComponent("SelectButtonJs").setIsTure(this.gameJs,false,questExp);
                    this.updatebuttonArrValue(i,false);
                    // node.parent = this.answersNode.getChildByName("picture");
                }
                if(i == 0 ||i== 1){
                    node.parent = this.answersNode.getChildByName("picture").getChildByName("lay1");
                }else{
                    node.parent = this.answersNode.getChildByName("picture").getChildByName("lay2");
                }
                this.updateButtonArr(node);
            }
        // }else{
        //     Log("没有那么多有图片的单词啊，我也乜办法");
        //     this.selectWordByVoice(quest);
        // }

    }
    selectPictureByVoice(wordsArr){//声音选  四张图
        var questExp = 7;
        this.voice.active = true;
        // if(wordsArr.length > 4){
            var quest = JayUtil.shuffle(this.getAnserWords(wordsArr,this.answerNum));
            var index = Math.floor(Math.random()*quest.length);
            //播放声音
            for(let i = 0;i < quest.length;i ++){
                if(quest[index].wordId == quest[i].wordId){
                    var node = cc.instantiate(this.answerPic);
                    let container = node.getChildByName("pic").getComponent(cc.Sprite);
                    this.voiceWord = quest[i].words;
                    ServerCenter.getWordVoice(this.bookId,this.unit,this.voiceWord);
                    // LoadUtil.LoadResImg(container,quest[i].wordsImg);
                    ServerCenter.getWordImg(this.bookId,this.unit,container,this.voiceWord);
                    node.getComponent("SelectButtonJs").setIsTure(this.gameJs,true,questExp);
                    this.updatebuttonArrValue(i,true);
                    // node.parent = this.answersNode.getChildByName("picture");
                }else{
                    var node = cc.instantiate(this.answerPic);
                    let container = node.getChildByName("pic").getComponent(cc.Sprite);
                    // LoadUtil.LoadResImg(container,quest[i].wordsImg);
                    ServerCenter.getWordImg(this.bookId,this.unit,container,quest[i].words);
                    node.getComponent("SelectButtonJs").setIsTure(this.gameJs,false,questExp);
                    this.updatebuttonArrValue(i,false);
                    // node.parent = this.answersNode.getChildByName("picture");
                }
                this.updateButtonArr(node);
                if(i == 0 ||i== 1){
                    node.parent = this.answersNode.getChildByName("picture").getChildByName("lay1");
                }else{
                    node.parent = this.answersNode.getChildByName("picture").getChildByName("lay2");
                }
            }
        // }else{
        //     Log("没有那么多有图片的单词啊，我也乜办法");
        //     this.selectWordByVoice(quest);
        // }

    }

    read(){
        //再次播放音频
    }

    getAnserWords(wordsList,number){
        if(number>Object.keys(wordsList).length){
           return;
        }   
        var tempArr=wordsList.slice(0);
        var newArrList=[];    
        for(var i=0;i<number;i++){
            var random=Math.floor(Math.random()*(tempArr.length-1));
            var arr=tempArr[random];
            tempArr.splice(random, 1);
            newArrList.push(arr);    
        }
        let noRepestArr = this.checkRepeat(newArrList);
        return JayUtil.shuffle(noRepestArr);

    }
    checkRepeat(arr){
        let newArr = [];
        for(var i in arr) {
            if(newArr.indexOf(arr[i]) == -1) {
            newArr.push(arr[i])
            }
        }
        return newArr;
    }

    selectWord(wordsList){
        var  wordsArr = [];
        for(let i =0;i<Object.keys(wordsList).length;i++){
            if(wordsList[i].wordsImg != ""){
                wordsArr.push(wordsList[i]);
            }
        }
        return this.checkRepeat(wordsArr);

    }





    
    

}
